package cz.uhk.max.service.impl;
import java.io.ByteArrayOutputStream;
import java.io.File; 
import java.io.IOException;
import java.util.Date; 

import com.sun.facelets.FaceletContext;

import cz.uhk.max.util.DateUtils;
import cz.uhk.max.util.FileUtils;
import cz.uhk.max.matrix.IEquationResult;
import cz.uhk.max.model.XLSFileData;
import cz.uhk.max.service.IXLSService;
import jxl.*;
import jxl.read.biff.BiffException;
import jxl.write.Label;
import jxl.write.Number;
import jxl.write.WritableCell;
import jxl.write.WritableCellFeatures;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;

public class XLSServiceImpl implements IXLSService {
	public XLSFileData getMatrices4x3Xls(IEquationResult matrices, String path) throws JXLException, IOException {
		Workbook wb  = Workbook.getWorkbook(new File(path + "/matrices_4x3.xls")); 
		
		ByteArrayOutputStream wbcopy = new ByteArrayOutputStream();
		WritableWorkbook workbook = Workbook.createWorkbook(wbcopy, wb);
		modifyXls(workbook,matrices);
	    workbook.write();
	    workbook.close();
	    
		XLSFileData data = new XLSFileData();
		data.setFileData(wbcopy.toByteArray());
		data.setContentType("application/vnd.ms-excel");
		data.setName("matrices_4x3_" + DateUtils.date2String(new Date(), "yyyy_MM_dd_HH_ss") + ".xls");
		data.setSize(data.getFileData().length);
		return data;
	}
	
	private void modifyXls(WritableWorkbook w, IEquationResult matrices) throws WriteException {
		WritableSheet sheet = w.getSheet("Matrices");
	    
	    WritableCell cell = null;
	    
	 // Change the contents of cell C1 to a11
	    cell = sheet.getWritableCell(2,0);
	    if (cell.getType() == CellType.NUMBER)
	    {
	      Number n = (Number) cell;
	      n.setValue(matrices.getA().getCol(0, 0).getValue());
	    }
	 // Change the contents of cell D1 to a12
	    cell = sheet.getWritableCell(3,0);
	    if (cell.getType() == CellType.NUMBER)
	    {
	      Number n = (Number) cell;
	      n.setValue(matrices.getA().getCol(0, 1).getValue());
	    }
	 
	  // Change the contents of cell E1 to a13
	    cell = sheet.getWritableCell(4,0);
	    if (cell.getType() == CellType.NUMBER)
	    {
	      Number n = (Number) cell;
	      n.setValue(matrices.getA().getCol(0, 2).getValue());
	    }
	 // Change the contents of cell C2 to a21
	    cell = sheet.getWritableCell(2,1);
	    if (cell.getType() == CellType.NUMBER)
	    {
	      Number n = (Number) cell;
	      n.setValue(matrices.getA().getCol(1, 0).getValue());
	    }
	 // Change the contents of cell D2 to a22
	    cell = sheet.getWritableCell(3,1);
	    if (cell.getType() == CellType.NUMBER)
	    {
	      Number n = (Number) cell;
	      n.setValue(matrices.getA().getCol(1, 1).getValue());
	    }
	 
	  // Change the contents of cell E2 to a23
	    cell = sheet.getWritableCell(4,1);
	    if (cell.getType() == CellType.NUMBER)
	    {
	      Number n = (Number) cell;
	      n.setValue(matrices.getA().getCol(1, 2).getValue());
	    }
	 // Change the contents of cell C3 to a31
	    cell = sheet.getWritableCell(2,2);
	    if (cell.getType() == CellType.NUMBER)
	    {
	      Number n = (Number) cell;
	      n.setValue(matrices.getA().getCol(2, 0).getValue());
	    }
	 // Change the contents of cell D3 to a32
	    cell = sheet.getWritableCell(3,2);
	    if (cell.getType() == CellType.NUMBER)
	    {
	      Number n = (Number) cell;
	      n.setValue(matrices.getA().getCol(2, 1).getValue());
	    }
	 
	  // Change the contents of cell E3 to a33
	    cell = sheet.getWritableCell(4,2);
	    if (cell.getType() == CellType.NUMBER)
	    {
	      Number n = (Number) cell;
	      n.setValue(matrices.getA().getCol(2, 2).getValue());
	    }
	 // Change the contents of cell C4 to a41
	    cell = sheet.getWritableCell(2,3);
	    if (cell.getType() == CellType.NUMBER)
	    {
	      Number n = (Number) cell;
	      n.setValue(matrices.getA().getCol(3, 0).getValue());
	    }
	 // Change the contents of cell D4 to a42
	    cell = sheet.getWritableCell(3,3);
	    if (cell.getType() == CellType.NUMBER)
	    {
	      Number n = (Number) cell;
	      n.setValue(matrices.getA().getCol(3, 1).getValue());
	    }
	 
	  // Change the contents of cell E4 to a43
	    cell = sheet.getWritableCell(4,3);
	    if (cell.getType() == CellType.NUMBER)
	    {
	      Number n = (Number) cell;
	      n.setValue(matrices.getA().getCol(3, 2).getValue());
	    }
	    
	    
	    // Change the contents of cell C6 to b11
	    cell = sheet.getWritableCell(2,5);
	    if (cell.getType() == CellType.NUMBER)
	    {
	      Number n = (Number) cell;
	      n.setValue(matrices.getB().getCol(0, 0).getValue());
	    }
	 // Change the contents of cell D6 to b12
	    cell = sheet.getWritableCell(3,5);
	    if (cell.getType() == CellType.NUMBER)
	    {
	      Number n = (Number) cell;
	      n.setValue(matrices.getB().getCol(0, 1).getValue());
	    }
	 
	  // Change the contents of cell E6 to b13
	    cell = sheet.getWritableCell(4,5);
	    if (cell.getType() == CellType.NUMBER)
	    {
	      Number n = (Number) cell;
	      n.setValue(matrices.getB().getCol(0, 2).getValue());
	    }
	 // Change the contents of cell C7 to b21
	    cell = sheet.getWritableCell(2,6);
	    if (cell.getType() == CellType.NUMBER)
	    {
	      Number n = (Number) cell;
	      n.setValue(matrices.getB().getCol(1, 0).getValue());
	    }
	 // Change the contents of cell D7 to b22
	    cell = sheet.getWritableCell(3,6);
	    if (cell.getType() == CellType.NUMBER)
	    {
	      Number n = (Number) cell;
	      n.setValue(matrices.getB().getCol(1, 1).getValue());
	    }
	 
	  // Change the contents of cell E7 to b23
	    cell = sheet.getWritableCell(4,6);
	    if (cell.getType() == CellType.NUMBER)
	    {
	      Number n = (Number) cell;
	      n.setValue(matrices.getB().getCol(1, 2).getValue());
	    }
	 // Change the contents of cell C8 to b31
	    cell = sheet.getWritableCell(2,7);
	    if (cell.getType() == CellType.NUMBER)
	    {
	      Number n = (Number) cell;
	      n.setValue(matrices.getB().getCol(2, 0).getValue());
	    }
	 // Change the contents of cell D8 to b32
	    cell = sheet.getWritableCell(3,7);
	    if (cell.getType() == CellType.NUMBER)
	    {
	      Number n = (Number) cell;
	      n.setValue(matrices.getB().getCol(2, 1).getValue());
	    }
	 
	  // Change the contents of cell E8 to a33
	    cell = sheet.getWritableCell(4,7);
	    if (cell.getType() == CellType.NUMBER)
	    {
	      Number n = (Number) cell;
	      n.setValue(matrices.getB().getCol(2, 2).getValue());
	    }
	 // Change the contents of cell C9 to b41
	    cell = sheet.getWritableCell(2,8);
	    if (cell.getType() == CellType.NUMBER)
	    {
	      Number n = (Number) cell;
	      n.setValue(matrices.getB().getCol(3, 0).getValue());
	    }
	 // Change the contents of cell D9 to b42
	    cell = sheet.getWritableCell(3,8);
	    if (cell.getType() == CellType.NUMBER)
	    {
	      Number n = (Number) cell;
	      n.setValue(matrices.getB().getCol(3, 1).getValue());
	    }
	 
	  // Change the contents of cell E9 to b43
	    cell = sheet.getWritableCell(4,8);
	    if (cell.getType() == CellType.NUMBER)
	    {
	      Number n = (Number) cell;
	      n.setValue(matrices.getB().getCol(3, 2).getValue());
	    }
	    
	    // Change the contents of cell C11 to x11
	    cell = sheet.getWritableCell(2,10);
	    if (cell.getType() == CellType.NUMBER)
	    {
	      Number n = (Number) cell;
	      n.setValue(matrices.getX().getCol(0, 0).getValue());
	    }
	 // Change the contents of cell D11 to x21
	    cell = sheet.getWritableCell(3,10);
	    if (cell.getType() == CellType.NUMBER)
	    {
	      Number n = (Number) cell;
	      n.setValue(matrices.getX().getCol(1, 0).getValue());
	    }
	 
	  // Change the contents of cell E11 to x31
	    cell = sheet.getWritableCell(4,10);
	    if (cell.getType() == CellType.NUMBER)
	    {
	      Number n = (Number) cell;
	      n.setValue(matrices.getX().getCol(2, 0).getValue());
	    }
	}
}
